package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.view.View;

import com.qs.base.utils.PhotoImgUtil;
import com.qs.home.entity.BackResultEntity;
import com.qs.home.entity.DriveEntity;
import com.qs.home.entity.TextResultEntity;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 驾驶证识别结果
 *
 * @Author ltzz
 * @Date 2019/9/5
 */
public class DriveDetailsViewModel extends BaseViewModel {
    public DriveDetailsViewModel(@NonNull Application application) {
        super(application);
    }

    //识别数据
    public ObservableField<TextResultEntity> textResultOb = new ObservableField<>();
    public ObservableField<DriveEntity> driveEntity = new ObservableField<>();

    //驾驶证是否可编辑
    public ObservableBoolean isFocusable = new ObservableBoolean(false);
    //驾驶证数据
    public ObservableField<String> zh = new ObservableField<>("");//证号
    public ObservableField<String> yxqx = new ObservableField<>("");//有效期限
    public ObservableField<String> zjcx = new ObservableField<>("");//准驾车型
    public ObservableField<String> zz = new ObservableField<>("");//住址
    public ObservableField<String> xm = new ObservableField<>("");//姓名
    public ObservableField<String> gj = new ObservableField<>("");//国籍
    public ObservableField<String> csrq = new ObservableField<>("");//出生日期
    public ObservableField<String> xb = new ObservableField<>("");//性别
    public ObservableField<String> cc = new ObservableField<>("");//初次领证日期
    //驾驶证历史数据
    public ObservableField<String> ls_zh = new ObservableField<>("");//证号
    public ObservableField<String> ls_yxqx = new ObservableField<>("");//有效期限
    public ObservableField<String> ls_zjcx = new ObservableField<>("");//准驾车型
    public ObservableField<String> ls_zz = new ObservableField<>("");//住址
    public ObservableField<String> ls_xm = new ObservableField<>("");//姓名
    public ObservableField<String> ls_gj = new ObservableField<>("");//国籍
    public ObservableField<String> ls_csrq = new ObservableField<>("");//出生日期
    public ObservableField<String> ls_xb = new ObservableField<>("");//性别
    public ObservableField<String> ls_cc = new ObservableField<>("");//初次领证日期
    //驾驶证文本
    public String driveText;
    //文字是否编辑
    public ObservableInt textHind = new ObservableInt(View.GONE);
    //底部是否编辑
    public ObservableInt botHind = new ObservableInt(View.VISIBLE);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //复制文本
        public ObservableField<String> copyTextUc = new ObservableField<>("");
        //翻译导出按钮
        public ObservableBoolean exportUc = new ObservableBoolean(false);
    }

    //初始化
    public void init() {
        zh.set(driveEntity.get().getWords_result().get("证号").get("words") == null ? "" : driveEntity.get().getWords_result().get("证号").get("words"));
        yxqx.set(driveEntity.get().getWords_result().get("有效期限").get("words") == null ? "" : driveEntity.get().getWords_result().get("有效期限").get("words"));
        zjcx.set(driveEntity.get().getWords_result().get("准驾车型").get("words") == null ? "" : driveEntity.get().getWords_result().get("准驾车型").get("words"));
        zz.set(driveEntity.get().getWords_result().get("住址").get("words") == null ? "" : driveEntity.get().getWords_result().get("住址").get("words"));
        xm.set(driveEntity.get().getWords_result().get("姓名").get("words") == null ? "" : driveEntity.get().getWords_result().get("姓名").get("words"));
        gj.set(driveEntity.get().getWords_result().get("国籍").get("words") == null ? "" : driveEntity.get().getWords_result().get("国籍").get("words"));
        csrq.set(driveEntity.get().getWords_result().get("出生日期").get("words") == null ? "" : driveEntity.get().getWords_result().get("出生日期").get("words"));
        xb.set(driveEntity.get().getWords_result().get("性别").get("words") == null ? "" : driveEntity.get().getWords_result().get("性别").get("words"));
        cc.set(driveEntity.get().getWords_result().get("初次领证日期").get("words") == null ? "" : driveEntity.get().getWords_result().get("初次领证日期").get("words"));
    }

    //复制
    public void copy() {
        ls_zh.set(zh.get());
        ls_yxqx.set(yxqx.get());
        ls_zjcx.set(zjcx.get());
        ls_zz.set(zz.get());
        ls_xm.set(xm.get());
        ls_gj.set(gj.get());
        ls_csrq.set(csrq.get());
        ls_xb.set(xb.get());
        ls_cc.set(cc.get());
    }

    //取消
    public void cancel() {
        zh.set(ls_zh.get());
        yxqx.set(ls_yxqx.get());
        zjcx.set(ls_zjcx.get());
        zz.set(ls_zz.get());
        xm.set(ls_xm.get());
        gj.set(ls_gj.get());
        csrq.set(ls_csrq.get());
        xb.set(ls_xb.get());
        cc.set(ls_cc.get());
    }


    //返回按钮
    public BindingCommand backClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });

    //保存按钮
    public BindingCommand saveClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PhotoImgUtil.saveBitmap(getApplication(), PhotoImgUtil.pathToBitmap(textResultOb.get().getFilePath()), "image");
            ToastUtils.showShort("已保存至相册");
        }
    });

    //驾驶证文本复制
    public BindingCommand bankTextClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            try {
                driveText = "证号：\n" + zh.get() + "\n" +
                        "\n有效期限：\n" + yxqx.get() + "\n" +
                        "\n准驾车型：\n" + zjcx.get() + "\n" +
                        "\n住址：\n" + zz.get() + "\n" +
                        "\n姓名：\n" + xm.get() + "\n" +
                        "\n国籍：\n" + gj.get() + "\n" +
                        "\n出生日期：\n" + csrq.get() + "\n" +
                        "\n性别：\n" + xb.get() + "\n" +
                        "\n初次领证日期：\n" + cc.get();
            } catch (Exception e) {
                driveText = "";
            }
            uc.copyTextUc.set(driveText);
        }
    });
    //驾驶证修改校对
    public BindingCommand bankModificationClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            copy();
            isFocusable.set(!isFocusable.get());
            textHind.set(View.VISIBLE);
            botHind.set(View.INVISIBLE);
        }
    });
    //驾驶证修改保存
    public BindingCommand bankTextSaveClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            isFocusable.set(!isFocusable.get());
            textHind.set(View.GONE);
            botHind.set(View.VISIBLE);
        }
    });
    //取消
    public BindingCommand bankCloseClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            cancel();
            isFocusable.set(!isFocusable.get());
            textHind.set(View.GONE);
            botHind.set(View.VISIBLE);
        }
    });

    //导出按钮
    public BindingCommand exportClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.exportUc.set(!uc.exportUc.get());
        }
    });
}
